package com.rmb.userinfo.controller;

import com.alibaba.fastjson.JSON;
import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.QueryResponseResult;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.invoice.InvoiceAddress;
import com.rmb.mart.entity.invoice.InvoiceInformation;
import com.rmb.userinfo.service.InvoiceAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 段段
 * @PackageName: com.rmb.userinfo.controller
 * @ClassName: InvoiceAddressController
 * @Description: (发票信息)表控制层
 * @data: 2021/1/20 15:18
 **/
@RestController
@RequestMapping("invoiceAddress")
public class InvoiceAddressController {
    /**
     * 服务对象
     */
    @Resource
    private InvoiceAddressService invoiceAddressService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public InvoiceAddress selectOne(Long id) {
        return this.invoiceAddressService.queryById(id);
    }

    /**
     * 查询发票地址所有信息
     * @return
     */
    @GetMapping("/selectAll")
    public QueryResponseResult selectAll(){
        List<InvoiceInformation> list = invoiceAddressService.selectAll();
        return new QueryResponseResult(CommonCode.QUERY_SUCCESS,list);
    }

    /**
     * 新增数据
     *
     * @param invoiceAddress 实例对象
     * @return 实例对象
     */
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody InvoiceAddress invoiceAddress){
        InvoiceAddress insert = invoiceAddressService.insert(invoiceAddress);
        return insert != null ?
                new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(invoiceAddress)) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("/deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id){
        boolean flag = invoiceAddressService.deleteById(id);
        return flag ? new ResponseResult(CommonCode.SUCCESS) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 修改数据
     *
     * @param invoiceAddress 实例对象
     * @return 实例对象
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody InvoiceAddress invoiceAddress){
        InvoiceAddress update = invoiceAddressService.update(invoiceAddress);
        return update != null ?
                new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(invoiceAddress)) :
                new ResponseResult(CommonCode.FAIL);
    }

}